package com.beijiteshop.shop.ui.coupon.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.base.adapter.showEmpty
import com.beijiteshop.libcommon.base.adapter.showError
import com.beijiteshop.libcommon.base.adapter.showLoading
import com.beijiteshop.libcommon.base.adapter.showNetError
import com.beijiteshop.libcommon.base.fragment.BaseLazyFragment
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.FragmentCouponListBinding
import com.beijiteshop.shop.model.api.response.Coupon
import com.beijiteshop.shop.ui.coupon.adapter.CouponAdapter
import com.beijiteshop.shop.ui.coupon.vm.CouponVM
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * 优惠券列表页面
 */
class CouponListFragment : BaseLazyFragment(R.layout.fragment_coupon_list) {

    companion object{

        private const val BUNDLE_TYPE = "bundle_type"
        private const val INIT_PAGE = 1

        fun newInstance(type: Int): CouponListFragment{
            return CouponListFragment().apply {
                arguments = Bundle().apply {
                    putInt(BUNDLE_TYPE,type)
                }
            }
        }
    }


    private val mBinding by bindView<FragmentCouponListBinding>()
    private val mCouponVM: CouponVM by viewModel()
    private var mType: Int? = null
    private val mAdapter = CouponAdapter(mutableListOf())
    private var mPage = INIT_PAGE

    override fun initView(view: View) {
        mType = arguments?.getInt(BUNDLE_TYPE)

        initRV()
    }

    override fun initData() {
    }

    override fun initEvent() {
        startObserve()
    }

    override fun onLazyLoad() {
        if (mType == null){
            return
        }
        initList()
    }

    private fun initRV(){

        mAdapter.apply {
            loadMoreModule.setOnLoadMoreListener {
                loadMoreData()
            }
        }

        mBinding.couponListRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mAdapter
        }
    }

    private fun initList(){
        mType?.let {
            mAdapter.showLoading()
            mCouponVM.getCoupons(mPage,it.toString())
        }
    }

    private fun loadMoreData(){
        mType?.let {
            mPage++
            mCouponVM.getCoupons(mPage,it.toString())
        }
    }

    private fun startObserve(){
        mCouponVM.apply {
            liveCoupons.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.setNewInstance(it)
                }else{
                    updateRVState(false,it)
                }
            }

            liveEmpty.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showEmpty(it){
                        initList()
                    }
                }else{
                    updateRVState(false,null)
                }
            }

            liveError.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveNetError.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showNetError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }
        }
    }

    /**
     * 更新列表加载状态
     */
    private fun updateRVState(error: Boolean,data: MutableList<Coupon>?){
        if (error){
            if (mAdapter.loadMoreModule.isLoading){
                mAdapter.loadMoreModule.loadMoreFail()
            }
        }else{
            if (mAdapter.loadMoreModule.isLoading){
                if (data.isNullOrEmpty()){
                    mAdapter.loadMoreModule.loadMoreEnd()
                }else{
                    mAdapter.addData(data)
                    mAdapter.loadMoreModule.loadMoreComplete()
                }
            }
        }
    }
}